<?php
/**
 * @author shutyuka@gmail.com
 * @modified 2011-02-20
 */
class ShopOrderLimit
{
	const SECRET_KEY = '@4!@#$%@';
	
	static public $STATUS_UNACTIVE = 0;
	
	static public $STATUS_ACTIVATE = 10;
	
	static public $TABLE_NAME="shop_order_limit";
	
	static public function GenPassword($p) {
		return md5($p . self::SECRET_KEY);
	}

	static public function Create($shopId, $serviceId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute, $limitCount = 1) {
		if( self::GetByComplex($shopId, $serviceId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute) != null){
			self::incOrderLimit($shopId, $serviceId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute);
			return ;
		}
		$shopOrderLimitRow = array();
		$shopOrderLimitRow['shop_id'] = $shopId;
		$shopOrderLimitRow['service_id'] = $serviceId;
		$shopOrderLimitRow['year'] = $year;
		$shopOrderLimitRow['month'] = $month;
		$shopOrderLimitRow['day'] = $day;
		$shopOrderLimitRow['start_hour'] = $startHour;
		$shopOrderLimitRow['start_minute'] = $startMinute;
		$shopOrderLimitRow['end_hour'] = $endHour;
		$shopOrderLimitRow['end_minute'] = $endMinute;
		$shopOrderLimitRow['create_time'] = time();
		$shopOrderLimitRow['limit_count'] = $limitCount;
		
		$shopOrderLimitRow['id'] = DB::Insert(self::$TABLE_NAME, $shopOrderLimitRow);
		
		return $shopOrderLimitRow['id'];
	}
	
	static public function GetByComplex($shopId, $serviceId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute) {
		$oc = array( 
			'shop_id' => $shopId, 
			"service_id" => $serviceId,
			"year" => $year,
			"month" => $month,
			"day" => $day,
			"start_hour = ".$startHour,
			"start_minute = ".$startMinute,
			"end_hour = ".$endHour,
			"end_minute = ".$endMinute,
			);
		$others = DB::LimitQuery(self::$TABLE_NAME, array(
					'condition' => $oc, 
					'one' => true,
					));
		return $others;
	}
	
	static public function incOrderLimit($shopId, $serviceId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute, $incCount = 1) {
		$tempStartHour = $startHour;
		$tempEndHour = $endHour;
		$tempStartMinute = $startMinute;
		$tempEndMinute = $endMinute;
		
		$shopOrderLimit = self::GetByComplex($shopId, $serviceId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute);
		if($shopOrderLimit == null){
			$shopOrderLimitRow['year'] = $year;
			$shopOrderLimitRow['month'] = $month;
			$shopOrderLimitRow['day'] = $day;
			$shopOrderLimitRow['start_hour'] = $startHour;
			$shopOrderLimitRow['start_minute'] = $startMinute;
			$shopOrderLimitRow['end_hour'] = $endHour;
			$shopOrderLimitRow['end_minute'] = $endMinute;
			$shopOrderLimitRow['limit_count'] = $shopOrderLimitRow['limit_count'] + $incCount;
		
			$shopOrderLimitRow['id'] = DB::Insert(self::$TABLE_NAME, $shopOrderLimitRow);
		}else if( $incCount < 0 ){
			$tempEndHour = $startHour + 1;
			$tempStartMinute = 0;
			$tempEndMinute = 0;
		}
		
		
		$oc = array( 
			'shop_id' => $shopId, 
			"service_id" => $serviceId,
			"year" => $year,
			"month" => $month,
			"day" => $day,
			"start_hour = ".$tempStartHour,
			"start_minute = ".$tempStartMinute,
			"end_hour = ".$tempEndHour,
			"end_minute = ".$tempEndMinute,
			);
		$others = DB::Update(self::$TABLE_NAME, $oc, array("limit_count" => "limit_count+$incCount"));
		return $others;
	}
	
	static public function incBookLimit($shopId, $serviceId, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute) {
		$oc = array( 
			'shop_id' => $shopId, 
			"service_id" => $serviceId,
			"year" => $year,
			"month" => $month,
			"day" => $day,
			"start_hour = ".$startHour,
			"start_minute = ".$startMinute,
			"end_hour = ".$endHour,
			"end_minute = ".$endMinute,
			);
		$others = DB::Update(self::$TABLE_NAME, $oc, array("booked_count" => "booked_count+1"));
		return $others;
	}
	

	
	static public function CanBook($shopId, $service, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute){
		/*$allOrderedCount = Preengage::GetOrderedCount($shopId, 0, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute);
		
		$allStaffCount = Staff::GetCountByTimeAndService($shopId, 0, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute);
		$allAbsenceCount = Staff::GetAbsenceCountByTimeAndService($shopId, 0, $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute);
		if( $allOrderedCount[0]['count'] >= $allStaffCount[0]['count'] - $allAbsenceCount[0]['count'] ){
			Logs::setLog("1  buke<".$allOrderedCount[0]['count']."> <".$allStaffCount[0]['count']."><".$allAbsenceCount[0]['count'].">");
			return -1;
		}
		$allDeviceCount = Devices::GetDeviceCountForService($shopId);
		if( $allOrderedCount[0]['count'] >= $allDeviceCount[0]['count'] ){
			Logs::setLog("2  buke<".$allOrderedCount[0]['count']."> <".$allDeviceCount[0]['count'].">");
			return -2;
		}
		
		$staffCount = Staff::GetCountByTimeAndService($shopId, $service['id'], $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute);
		$absenceCount = Staff::GetAbsenceCountByTimeAndService($shopId, $service['id'], $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute);
		$orderedCount = Preengage::GetOrderedCount($shopId, $service['id'], $year, $month, $day, $startHour, $startMinute, $endHour, $endMinute);
		
		$deviceCount = Devices::GetDeviceCountForService($shopId, $service['id']);
		if( $orderedCount[0]['count'] >= $deviceCount[0]['count'] ){
			Logs::setLog("4  buke<".$orderedCount[0]['count']."> <".$deviceCount[0]['count'].">");
			return -4;
		}*/
		
		return 1;
	}
	
	static public function incLimit($shopId, $serviceId, $orderStartTimeArray, $orderEndTimeArray, $count){
		$orderStartTime = mktime(
			$orderStartTimeArray["hour"], $orderStartTimeArray["minute"], $orderStartTimeArray["second"], 
			$orderStartTimeArray["month"], $orderStartTimeArray["day"], $orderStartTimeArray["year"]
		);
		$orderEndTime = mktime(
			$orderEndTimeArray["hour"], $orderEndTimeArray["minute"], $orderEndTimeArray["second"], 
			$orderEndTimeArray["month"], $orderEndTimeArray["day"], $orderEndTimeArray["year"]
		);
		$oc = array( 
			'shop_id' => $shopId, 
			"service_id" => $serviceId,
			"order_time < {$orderStartTime}",
			"order_time > {$orderEndTime}",
			);
		
		$table = new Table(self::$TABLE_NAME, $newuser);
		
		$others = DB::LimitQuery(self::$TABLE_NAME, array(
					'condition' => $oc, 
					'one' => true,
					));
		return $others;
	}
	
	static public function GetOtherOrderStaff($serviceId, $orderStartTime, $orderEndTime){
		$sql = "select distinct(p.designation_stuff) as staff_id, p.service_id from preengage p, staff_skill s".
		" where ".
		"p.service_id!=$serviceId and p.designation=0 and !($orderEndTime <= p.service_start_time or $orderStartTime >= p.real_end_time) ".
		" and p.designation_stuff=s.staff_id and s.service_id=$serviceId";
		return DB::GetQueryResult($sql, false);
	}
	
	
	static public function GetWorkedStaff($serviceId, $orderStartTime, $orderEndTime){
		//"select * from preengage where service_id in(select service_id from staff_skill where staff_id=2) and service_id!=2 and designation_stuff=2;"
		
		$sql = "select distinct(p.designation_stuff) from preengage p, staff_skill s where p.service_id=$serviceId and p.designation=0 and !(p.service_start_time<$orderStartTime or p.real_end_time>$orderEndTime)";
		
		return DB::GetQueryResult($sql, false);
	}
	
	static public function GetReplaceAbleStaff($serviceId, $orderStartTime, $orderEndTime, $sourceStaffId){
		$sql = "select s.staff_id from staff_skill s, absence a, work_schedule w".
		" where s.service_id=$serviceId and a.staff_id=s.staff_id and w.staff_id=s.staff_id and !($orderEndTime <= a.start_time or $orderStartTime >= p.start_time)";
		
		return DB::GetQueryResult($sql, false);
	}
	
	
}
